<template>
  <ga-modal
    v-model:visible="visible"
    title-align="start"
    :width="600"
    @close="handleCancel(formRef)"
  >
    <template #title>{{ curId ? "编辑" : "创建" }}</template>
    <ga-spin style="width: 100%" :loading="loading">
      <ga-form ref="formRef" :model="formData">
        <ga-form-item
          field="name"
          label="名称"
          label-col-flex="70px"
          :rules="[RULE.NOT_NULL]"
          :validate-trigger="['change', 'input']"
        >
          <ga-input v-model="formData.name" placeholder="请输入名称" />
        </ga-form-item>
        <ga-form-item
          field="type"
          :rules="[RULE.NOT_NULL]"
          :validate-trigger="['change', 'input']"
          label="类型"
          label-col-flex="70px"
        >
          <ga-radio-group type="button" v-model="formData.type" size="small">
            <ga-radio value="item.value"> 反亲和</ga-radio>
            <ga-radio value="item.value"> 弱亲和</ga-radio>
          </ga-radio-group>
        </ga-form-item>
        <ga-form-item field="remark" label="备注" label-col-flex="70px">
          <ga-textarea v-model="formData.remark" placeholder="请输入内容" />
        </ga-form-item>
      </ga-form>
    </ga-spin>
    <template #footer>
      <ga-button @click="visible = false">取消</ga-button>
      <ga-button type="primary" @click="handleOk">确定</ga-button>
    </template>
  </ga-modal>
</template>

<script setup>
import { ref, defineEmits, reactive } from "vue";
import { editImage } from "@/api/image";
import { Message } from "@garco-design/web-vue";
import { RULE } from "@/constant/validators";
import useLoading from "@/hooks/loading";

const { loading, setLoading } = useLoading(false);
const formRef = ref(null);
const formData = reactive({
  name: "",
  type: "",
  remark: "",
});

const visible = ref(false);
const curId = ref("");
const open = async (val) => {
  visible.value = true;
  if (val && val?.id) {
    setLoading(true);
    curId.value = val.id;
    Object.keys(formData).forEach((item) => {
      formData[item] = val[item];
    });
    setLoading(false);
  }
};
// 保存
const emits = defineEmits(["fetchData"]);
const handleOk = async () => {
  // const res = await formRef.value?.validate()
  // if (!res) {
  //   try {
  //     const { code } = !curId.value ? await createList(formData):await editList(curId.value, formData)
  //     if (code === 20000) Message.success('操作成功')
  //     emits('fetchData')
  //   } finally {
  //     visible.value = false
  //   }
  // }
};
const handleCancel = (formEl) => {
  curId.value = "";
  formEl.resetFields();
};
defineExpose({ open });
</script>

<style lang="scss" scoped></style>
